//
//  VKJiFenShopDetailPayController.m
//  V-Learn
//
//  Created by liubo on 2019/1/9.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKJiFenShopDetailPayController.h"
#import "VKJiFenShopPayCell.h"
#import "VKJiFenShopPayModel.h"
#import "VKJiFenShopController.h"
@interface VKJiFenShopDetailPayController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) VKJiFenShopModel *model;
@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation VKJiFenShopDetailPayController
- (instancetype)initWithModel:(VKJiFenShopModel *)model
{
    self = [super init];
    if (self) {
        self.model = model;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付";
    self.view.backgroundColor = [UIColor whiteColor];
        [self tableView];
    //    [self setupDataArray];
    
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupUI];
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT - 50) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.rowHeight = 50;
        [self setupHeaderView];
        [self setupFooterView];
    }
    return _tableView;
}
- (void)setupFooterView{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 108)];
    bgView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [UIView new];
}
- (void)setupHeaderView{
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 158)];
    bgView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableHeaderView = bgView;
    
    UIView *topView = [[UIView alloc]init];
    [bgView addSubview:topView];
    topView.backgroundColor = [UIColor cz_colorWithRed:243 green:243 blue:243];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bgView);
        make.left.right.mas_equalTo(bgView);
        make.height.equalTo(@10);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    [bgView addSubview:titleLabel];
    titleLabel.text = @"商品名称";
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.textColor = [UIColor cz_colorWithRed:40 green:40 blue:40];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(topView).offset(15);        make.top.mas_equalTo(topView.mas_bottom).offset(15);
    }];
    
    UILabel *jifenLabel = [[UILabel alloc]init];
    [bgView addSubview:jifenLabel];
    jifenLabel.text = @"积分";
    jifenLabel.font = [UIFont systemFontOfSize:17];
    jifenLabel.textColor = [UIColor cz_colorWithRed:40 green:40 blue:40];
    [jifenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(15);
    }];
    
    UILabel *goodNameLabel = [[UILabel alloc]init];
    [bgView addSubview:goodNameLabel];
    goodNameLabel.text = self.model.goodsName;
    goodNameLabel.textColor = [UIColor cz_colorWithRed:40 green:40 blue:40];
    [goodNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.right.mas_equalTo(topView.mas_right).offset(-15);
    }];
    
    UILabel *countLabel = [[UILabel alloc]init];
    [bgView addSubview:countLabel];
    countLabel.text = [NSString stringWithFormat:@"%@积分",self.model.goodsIntegral];
    countLabel.textColor = [UIColor cz_colorWithRed:40 green:40 blue:40];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(jifenLabel);
        make.right.mas_equalTo(topView.mas_right).offset(-15);
    }];
    
    if ([self.model.goodsType isEqualToString:@"0"]){// 实体

        UIView *bgV = [[UIView alloc]init];
        [bgView addSubview:bgV];
        bgV.backgroundColor = [UIColor cz_colorWithRed:243 green:243 blue:243];
        [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(bgView);
            make.height.equalTo(@10);
            make.top.mas_equalTo(jifenLabel.mas_bottom).offset(15);
        }];
        
        UILabel *infoLabel = [[UILabel alloc]init];
        [bgView addSubview:infoLabel];
        infoLabel.text = @"收货信息";
        infoLabel.font = [UIFont systemFontOfSize:17];
        infoLabel.textColor = [UIColor cz_colorWithRed:40 green:40 blue:40];
        [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.view).offset(15);                make.top.mas_equalTo(bgV.mas_bottom).offset(17);
        }];
    }
}
- (void)setupUI{
    
    UIView *downView = [[UIView alloc]init];
    [self.view addSubview:downView];
    downView.backgroundColor = [UIColor cz_colorWithRed:249 green:249 blue:249];
    [downView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.left.right.equalTo(@0);
        make.height.equalTo(@50);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [downView addSubview:button];
    button.backgroundColor = [UIColor cz_colorWithRed:235 green:81 blue:32];
    [button setTitle:@"确认支付" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.right.mas_equalTo(downView);
        make.width.equalTo(@120);
    }];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *moneyLabel = [[UILabel alloc]init];
    [downView addSubview:moneyLabel];
    moneyLabel.text = [NSString stringWithFormat:@"支付积分: %@",self.model.goodsIntegral];
    moneyLabel.textColor = [UIColor cz_colorWithRed:253 green:79 blue:83];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(downView);
        make.right.mas_equalTo(button.mas_left).offset(-20);
    }];
    
}
- (void)buttonClick{
    if ([self.model.goodsType isEqualToString:@"1"]){// 代金券
        NSMutableDictionary *pagramsD = @{@"token":[UserModel getUserModel].token,@"goodsName":self.model.goodsName,@"goodsId":self.model.goodsId,@"voucherAmount":self.model.goodsIntegral,@"goodsIntegral":self.model.goodsIntegral}.mutableCopy;
        [WYHXNetWorkService netWorkSerViceRequestWithPostUrl:WKStudyMyIntegralVoucherExchange withPostParams:pagramsD withBlock:^(NSDictionary *result, NSError *error) {
            
            if ([result[@"code"]integerValue] == 200) {
                
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                  hud.labelText = @"支付成功";
                   [[[UIApplication sharedApplication].windows lastObject]addSubview:hud];
                [hud show:YES];

                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
                //显示2秒隐藏
                [hud hide:YES afterDelay:2];
                
            }else if([result[@"code"]integerValue]==500){
                [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
            }
            
        }];
        
        return;
    }else{
        UITextField *nameTf = [self.view viewWithTag:2000 + 0];
        UITextField *iphoneTf = [self.view viewWithTag:2000 + 1];
        UITextField *addressTf = [self.view viewWithTag:2000 + 2];
        
        NSString *name = [nameTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *iphone = [iphoneTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *address = [addressTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        if (name.length == 0) {
            [MBProgressHUD showSuccess:nameTf.placeholder toView:self.view];
            return;
        }
        
        if (iphone.length == 0) {
            [MBProgressHUD showSuccess:iphoneTf.placeholder toView:self.view];
            return;
        }
        if (![self checkTelNumber:iphone]) {
            [MBProgressHUD showSuccess:@"手机号格式不正确" toView:self.view];
            return;
        }
        if (address.length == 0) {
            [MBProgressHUD showSuccess:addressTf.placeholder toView:self.view];
            return;
        }
        
        
        NSMutableDictionary *pagramsD = @{@"token":[UserModel getUserModel].token,@"goodsName":self.model.goodsName,@"goodsId":self.model.goodsId,@"goodsPrice":self.model.goodsIntegral,@"tel":iphoneTf.text,@"username":nameTf.text,@"address":addressTf.text}.mutableCopy;
        [WYHXNetWorkService netWorkSerViceRequestWithPostUrl:WKStudyMyIntegralGoodsExchange withPostParams:pagramsD withBlock:^(NSDictionary *result, NSError *error) {
            
            if ([result[@"code"]integerValue] == 200) {
                NSString *msg = result[@"msg"];
                //            LBPayVC *payVC = [[LBPayVC alloc]init];
                //            LBCourseOutputDTOListModel *model = [[LBCourseOutputDTOListModel alloc]init];
                //            model.courseName = @"主题课程第一季";
                //            model.coursePrice = @"2000";
                //            payVC.model = model;
                //            [self.navigationController pushViewController:payVC animated:YES];
                //和后台确认这里直接支付成功后返回
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                hud.mode = MBProgressHUDModeText;
                hud.labelText = @"支付成功";
                [[[UIApplication sharedApplication].windows lastObject]addSubview:hud];
                [hud show:YES];
                
                [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
                //显示2秒隐藏
                [hud hide:YES afterDelay:2];
//
                
//                for (UIViewController *controller in self.navigationController.viewControllers) {
//                    if ([controller isKindOfClass:[VKJiFenShopController class]]) {
//                        VKJiFenShopController *chooseSecurityVC =(VKJiFenShopController *)controller;
//                        [self.navigationController popToViewController:chooseSecurityVC animated:YES];
//                    }
//                }
                
            }else{
                [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
            }
            
        }];
    }
    
    
}
- (BOOL)checkTelNumber:(NSString *)elNumber{
    NSString *pattern =@"^1+[3578]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    BOOL isMatch = [pred evaluateWithObject:elNumber];
    
    return isMatch;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.model.goodsType isEqualToString:@"1"]){// 代金券
        return 0;
    }
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.model.goodsType isEqualToString:@"1"]){// 代金券
        return [[UITableViewCell alloc]init];
    }else{
        VKJiFenShopPayCell *cell = [VKJiFenShopPayCell cellWithTableView:tableView];
        VKJiFenShopPayModel *model = [[VKJiFenShopPayModel alloc]init];
        model.row = indexPath.row;
        if (indexPath.row == 0) {
            model.leftStr = @"姓名:";
            model.placeHoldStr = @"请输入收件人的姓名";
            cell.model= model;
        }else if (indexPath.row == 1){
            model.leftStr = @"联系电话:";
            model.placeHoldStr = @"请输入收件人手机号码";
            cell.model= model;
            
        }else if (indexPath.row == 2){
            model.leftStr = @"收货地址:";
            model.placeHoldStr = @"请输入收件人详细地址";
            cell.model= model;
        }
        
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
